const LogInfo = require("../models/logInfo");

module.exports = class LogManager {
  /**
   * 增加一个Log
   * @param {*} obj
   * @returns
   */
  static async add(obj) {
    const result = await LogInfo.create(obj);
    return result.toJSON();
  }

  /**
   * 删除一个Log
   * @param {*} id
   */
  static async delete(id) {
    const result = await LogInfo.destroy({
      where: {
        id,
      },
    });
  }

  /**
   * 更新Log
   * @param {*} Id
   * @param {*} Obj
   */
  static async update(Id, obj) {
    const result = await LogInfo.update(obj, {
      where: {
        id: Id,
      },
    });
  }

  /**
   * 分页查询Log
   * @returns
   */
  static async find(page = 1, limit = 10) {
    const result = await LogInfo.findAndCountAll({
      offset: (page - 1) * limit,
      limit: +limit,
      order: [["createdAt", "DESC"]],
    });
    if (result === null) {
      console.log("Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }

  /**
   * 查询所有Log
   * @returns
   */
  static async findAll() {
    const result = await LogInfo.findAndCountAll({
      order: [["createdAt", "DESC"]],
    });
    if (result === null) {
      console.log("Not found!");
    } else {
      return JSON.parse(JSON.stringify(result));
    }
  }
};
